
options(digits=4, width=70)

# make sure packages are installed prior to loading them
library(PerformanceAnalytics)
library(zoo)
library(boot)
library(tseries)

# get monthly adjusted closing price data on firstTicker, secondTicker and DAX from Yahoo
# using the tseries function get.hist.quote(). Set sample to Sept 2005 through
# Sep 2010. Note: if you are not careful with the start and end dates
# or if you set the retclass to "ts" then results might look weird

startDate="2012-08-12"
endDate="2014-03-12"
#firstTickerName="RTS"
#firstTickerYahooCode="RTS.RS"
firstTickerName="SP500"
firstTickerYahooCode="%5EGSPC"
secondTickerName="Yandex"
secondTickerYahooCode="YNDX"

# get the last five years of monthly adjusted closing prices from Yahoo!
firstTicker.prices = get.hist.quote(instrument=firstTickerYahooCode, start=startDate,
                              end=endDate, quote="AdjClose",
                              provider="yahoo", origin="1970-01-01",
                              compression="m", retclass="zoo")
# change class of time index to yearmon which is appropriate for monthly data
# index() and as.yearmon() are functions in the zoo package 
#                             
index(firstTicker.prices) = as.yearmon(index(firstTicker.prices))

class(firstTicker.prices)
colnames(firstTicker.prices)
start(firstTicker.prices)
end(firstTicker.prices)

secondTicker.prices = get.hist.quote(instrument=secondTickerYahooCode, start=startDate,
                                  end=endDate, quote="AdjClose",
                                  provider="yahoo", origin="1970-01-01",
                                  compression="m", retclass="zoo")
index(secondTicker.prices) = as.yearmon(index(secondTicker.prices))


# create merged price data
lab4Prices.z = merge(firstTicker.prices, secondTicker.prices)

# rename columns
colnames(lab4Prices.z) = c(firstTickerName, secondTickerName)

# calculate cc returns as difference in log prices
lab4Returns.z = diff(log(lab4Prices.z))

#
# 3. Create timePlots of data
#
plot(lab4Returns.z, plot.type="single", lty=1:3, col=1:2, lwd=2)
legend(x="bottomleft", legend=colnames(lab4Returns.z), lty=1:3, col=1:2, lwd=2)
abline(h=0)
title("Monthly cc returns")

#
# 4. Create matrix of return data and compute pairwise scatterplots
#

ret.mat = coredata(lab4Returns.z)
colnames(ret.mat)
head(ret.mat)
firstTicker = ret.mat[,firstTickerName]
secondTicker = ret.mat[,secondTickerName]
pairs(ret.mat, col="blue")

#
# 5. Compute estimates of CER model parameters
#
muhat.vals = apply(ret.mat, 2, mean)
muhat.vals
sigma2hat.vals = apply(ret.mat, 2, var)
sigma2hat.vals
sigmahat.vals = apply(ret.mat, 2, sd)
sigmahat.vals
cov.mat = var(ret.mat)
cov.mat
cor.mat = cor(ret.mat)
cor.mat
covhat.vals = cov.mat[lower.tri(cov.mat)]
rhohat.vals = cor.mat[lower.tri(cor.mat)]
names(covhat.vals) <- names(rhohat.vals) <- 
  c("firstTicker,secondTicker")
covhat.vals
rhohat.vals

# summarize the CER model estimates
cbind(muhat.vals,sigma2hat.vals,sigmahat.vals)
cbind(covhat.vals,rhohat.vals)

# plot mean vs. sd values
plot(sigmahat.vals, muhat.vals, pch=1:2, cex=2, col=1:3, 
     ylab = "mean", xlab="sd (risk)")
abline(h=0)     
legend(x="topright", legend=names(muhat.vals), pch=1:2, col=1:3, cex=1.5) 


#....
#
# 11. 24 month rolling estimates of mu and sd
#

# rolling analysis for firstTicker
roll.mu.firstTicker = rollapply(lab4Returns.z[,"firstTicker"],
                          FUN=mean, width = 24, align="right")

roll.sd.firstTicker = rollapply(lab4Returns.z[,"firstTicker"],
                          FUN=sd, width = 24, align="right")

plot(merge(roll.mu.firstTicker,roll.sd.firstTicker,lab4Returns.z[,"firstTicker"]), plot.type="single",
     main="24-month rolling means and sds for firstTicker", ylab="Percent per month",
     col=c("blue","red","black"), lwd=2)  
abline(h=0)
legend(x="bottomleft", legend=c("Rolling means","Rolling sds", "firstTicker returns"), 
       col=c("blue","red","black"), lwd=2)

# rolling analysis for secondTicker
roll.mu.secondTicker = rollapply(lab4Returns.z[,"secondTicker"],
                              FUN=mean, width = 24, align="right")

roll.sd.secondTicker = rollapply(lab4Returns.z[,"secondTicker"],
                              FUN=sd, width = 24, align="right")

plot(merge(roll.mu.secondTicker,roll.sd.secondTicker,lab4Returns.z[,"secondTicker"]), plot.type="single",
     main="24-month rolling means and sds for secondTicker", ylab="Percent per month",
     col=c("blue","red","black"), lwd=2)	
abline(h=0)
legend(x="bottomleft", legend=c("Rolling means","Rolling sds", "secondTicker returns"), 
       col=c("blue","red","black"), lwd=2)